/**
 * Created by WT-SUN on 2017/4/18.
 */
$(function () {
    $('#planTb').datagrid({
        rownumbers: true,//显示行号
        striped: true,
        fit: true,
        singleSelect: true,
        sortOrder: true,
        fitColumns: true,
        pagination: true,
        toolbar: '#toolbar',
        url: ROOT_PATH + '/occupationalHealth/medicalPlan/planTable.shtml',
        queryParams:{
            sort:'addTime',
            order:'desc'
        },
        columns: [[
            {
                field: 'id',
                checkbox: true
            },
            {field: 'batch', title: '批次', width: 100},
            {field: 'yearAnnual', title: '年度', width: 100},
            {field: 'medicalCategory', title: '体检类别', width: 100},
            {field: 'deptname', title: '登记部门', width: 100},
            {field: 'addTime', title: '登记时间', width: 100},
        ]]
    });
    $('#btn_clear').on("click", function () {
        $('#queryForm').form('reset');
    });
    queryDepartmentTree();
    queryYearthList("yearAnnual");
    $('#btn_save').on("click", function () {
            var $selected = $('#planTb').datagrid('getSelections');
            if($selected.length==0){
                layer.alert("请选体检批次！");
                return;
            }
            WindowManager.closeLayer({
                returnValue: $selected
            });
    });
});
/**
 * 查询部门树方法
 */
function queryDepartmentTree(){
    $.ajax({
        url:ROOT_PATH + '/permission/department/getDepartmentTree.shtml',//提交地址
        data:{parentId : 0},//将表单数据序列化
        type:"POST",
        dataType:"json",
        success:function(result){
            $('#companyName').combotree(
                {
                    data:result,
                    require:true,
                    loadFilter:function(data){
                        var newData = new Array();
                        newData.push({id:"",text:'请选择'});
                        newData.push(data[0]);
                        return newData;
                    }
                }
            );
        }
    });
}
/**
 * 重新加载表格
 */
function reLoadData() {
    $('#planTb').datagrid('clearSelections');
    var queryParams = $('#queryForm').serializeJson();
    queryParams["sort"] = 'addTime';
    queryParams["order"] = 'desc';
    $('#planTb').datagrid('options').queryParams = queryParams;
    $("#planTb").datagrid('reload', ROOT_PATH + '/occupationalHealth/medicalPlan/planTable.shtml');
}

function queryYearthList(id){
    var date = new Date();
    var year = date.getFullYear();
    var yearthData = new Array();
    yearthData.push({id:"",text:"请选择"});
    for(var i =0;i<6;i++){
        var y = {id:year-i,text:(year-i)+'年'};
        yearthData.push(y);
    }
    $('#'+id).combobox(
        {
            data:yearthData,
            valueField:'id',
            textField:'text'
        }
    );
}
